﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sherwood.SignOn.Client.Session;

namespace Sherwood.SignOn.Client.Tests
{
    [TestClass]
    public class SessionRepositoryTests
    {
        [TestMethod]
        public void Add_And_Check_Client_Session_Test()
        {
            //Arrange
            ISessionRepository repository = new SqlSessionRepository();

            //Act
            string sessionId = repository.CreateSession();
            bool validSession = repository.SessionExists(sessionId);

            //Assert
            Assert.IsTrue(validSession, "Session should be valid!");
        }

        [TestMethod]
        public void Add_And_Check_Non_Existing_Session_Test()
        {
            //Arrange
            ISessionRepository repository = new SqlSessionRepository();

            //Act
            string sessionId = repository.CreateSession();
            bool validSession = repository.SessionExists(Guid.NewGuid().ToString());

            //Assert
            Assert.IsFalse(validSession, "Session should not be valid!");
        }

        [TestMethod]
        public void Add_And_Check_Deleted_Session_Test()
        {
            //Arrange
            ISessionRepository repository = new SqlSessionRepository();

            //Act
            string sessionId = repository.CreateSession();
            repository.DeleteSession(sessionId);
            bool validSession = repository.SessionExists(sessionId);

            //Assert
            Assert.IsFalse(validSession, "Session should have been deleted!");
        }


        [TestMethod]
        public void Cleanup_Test()
        {
            //Arrange
            ISessionRepository repository = new SqlSessionRepository();

            //Act
            string sessionId = repository.CreateSession();
            repository.CleanUp();

            //Assert
            //Just make sure no exception is thrown.
        }

        //skip
        public void Basic_Performance_Test()
        {
            //Arrange
            ISessionRepository repository = new SqlSessionRepository();
            string sessionId = "";
            DateTime startTime = DateTime.Now;

            //Act
            for (int i = 0; i < 10; i++)
            {
                sessionId = repository.CreateSession();
                for (int j = 0; j < 50; j++)
                {
                    bool validSession = repository.SessionExists(sessionId);
                    bool activatedSession = repository.SessionActive(sessionId);
                }
                repository.ActivateSession(sessionId);
                for (int j = 0; j < 50; j++)
                {
                    bool validSession = repository.SessionExists(sessionId);
                    bool activatedSession = repository.SessionActive(sessionId);
                }
            }
            repository.CleanUp();

            Console.WriteLine("10 session creations and activations and 2000 session checks took: " + (DateTime.Now - startTime).TotalSeconds.ToString() + " seconds");

            //Assert
            //Just make sure no exception is thrown.
        }
    }
}
